package com.example.xmpic.ui.activity.bind;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivityBindScanBinding;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.OnFunDeviceListener;
import com.example.xmpic.support.models.FunDevStatus;
import com.example.xmpic.support.models.FunDevType;
import com.example.xmpic.support.models.FunDevice;
import com.example.xmpic.support.models.FunLoginType;
import com.example.common.base.BasePresentActivity;
import com.example.xmpic.mvp.contract.CameraIncreaseContract;
import com.example.xmpic.mvp.present.CameraIncreasePresenter;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author skygge
 * @date 2019-11-27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：扫码添加摄像头
 */
public class CameraConfigScanActivity extends BasePresentActivity<CameraIncreasePresenter, ActivityBindScanBinding> implements OnFunDeviceListener, CameraIncreaseContract.IView {

	private String mCurrDevSn = null;
	private FunDevType mCurrDevType = null;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_bind_scan;
	}

	@Override
	protected void initPresent() {
		super.initPresent();
		mPresent = new CameraIncreasePresenter(mContext);
	}

	@Override
	protected void initView() {
		super.initView();
		// 设置登录方式为本地登录
		FunSupport.getInstance().setLoginType(FunLoginType.LOGIN_BY_LOCAL);
		// 监听设备类事件
		FunSupport.getInstance().registerOnFunDeviceListener(this);
		mCurrDevType = FunDevType.EE_DEV_NORMAL_MONITOR;
	}

	@Override
	protected void initListener() {
		super.initListener();
		mDBind.btnWifiQuickAdd.setOnClickListener(v -> startSNAddResult());
		mDBind.btnScanCode.setOnClickListener(v -> startActivityForResult(new Intent(this, ScanQRCodeActivity.class),1));
		mDBind.ivWifiConfigBack.setOnClickListener(v -> finish());
	}

	private void startSNAddResult(){
		showProgressDialog();
		String code = mDBind.editSN.getInputContent();
		if(TextUtils.isEmpty(code)){
			dismissProgressDialog();
			showToast(getResources().getString(R.string.camera_empety_sn));
			return;
		}
		mCurrDevSn = code;
		FunSupport.getInstance().requestDeviceStatus(mCurrDevType, code);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode!= Activity.RESULT_OK) return;

		if(requestCode==1){
			Pattern pattern = Pattern.compile("^[A-Za-z0-9]{16}$");
			String sn = Objects.requireNonNull(data.getExtras()).getString("SN");
			Matcher m = pattern.matcher(sn);
			if( !m.matches() ){ //匹配不到,說明輸入的不符合條件
              showToast(getString(R.string.sn_is_illegal));
			}else{
				if(!TextUtils.isEmpty(sn))
					mDBind.editSN.setInputContent(sn);
			}
		}
	}

	@Override
	public void hasExits() {
		dismissProgressDialog();
		showToast(getString(R.string.device_has_been_exist));
	}

	@Override
	public void showToastMsg(String msg) {
		dismissProgressDialog();
		showToast(msg);
	}

	@Override
	public void bindSuccess() {
		dismissProgressDialog();
		showToast(getString(R.string.success));
		finish();
	}

	@Override
	public void deleteSuccess() {

	}

	@Override
	public void onDeviceListChanged() {

	}

	@Override
	public void onDeviceStatusChanged(FunDevice funDevice) {
		if (null != mCurrDevSn && mCurrDevSn.equals(funDevice.getDevSn())) {
			if (funDevice.devStatus == FunDevStatus.STATUS_ONLINE) {
				// 如果设备在线,获取设备信息
				mPresent.refreshModifyUserInfo(funDevice.getDevSn(), funDevice.getDevName());
			}else {
				showToastMsg(getString(R.string.device_not_exist));
			}
		}else {
			showToastMsg(getString(R.string.device_not_exist));
		}
	}

	@Override
	public void onDeviceAddedSuccess() {

	}

	@Override
	public void onDeviceAddedFailed(Integer errCode) {

	}

	@Override
	public void onDeviceRemovedSuccess() {

	}

	@Override
	public void onDeviceRemovedFailed(Integer errCode) {

	}

	@Override
	public void onAPDeviceListChanged() {

	}

	@Override
	public void onLanDeviceListChanged() {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 注销设备事件监听
		FunSupport.getInstance().removeOnFunDeviceListener(this);
	}
}
